package com.fzy.core.acegi;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;

import com.fzy.core.BaseService;
import com.fzy.core.utils.RsUtils;
import com.fzy.core.utils.U;

public class ResourceService extends BaseService{

	public List getResources(){
		String sql1 = "select resource_id, role_id, rolename, roledesc  from tdn_recruit_res_role_rel a left join auth_rolemsg r on a.role_id = r.roleid where a.status = '1'";
		final List resourceRoleRelations = queryForList(sql1);
		
		String sql = "select * from tdn_recruit_resource where status = '1'";
		return query(sql, new RowMapper(){
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Resource entity = new Resource();
				entity.setId(RsUtils.getLong("ID", rs));
				entity.setResourceName(rs.getString("RESOURCE_NAME"));
				entity.setTitle(rs.getString("TITLE"));
				entity.setResourceString(rs.getString("RESOURCE_STRING"));
				entity.setStatus(rs.getString("STATUS"));
				entity.setCreatedUser(RsUtils.getLong("CREATED_USER", rs));
				entity.setCreatedDate(rs.getDate("CREATED_DATE"));
				entity.setUpdatedUser(RsUtils.getLong("UPDATED_USER", rs));
				entity.setUpdatedDate(rs.getDate("UPDATED_DATE"));
				
				for (Iterator iterator = resourceRoleRelations.iterator(); iterator
						.hasNext();) {
					Map m = (Map) iterator.next();
					BigDecimal resourceId = (BigDecimal)m.get("resource_id");
					if(resourceId != null && resourceId.longValue() == entity.getId().longValue()){
						List roles = entity.getRoleNameList();
						if(roles == null) roles = new ArrayList();
						roles.add(m.get("rolename"));
						entity.setRoleNameList(roles);
					}
				}
				return entity;
			}});
	}
}
